<?php

/* 充值规则 */
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;

class ChargeruleController extends AdminBaseController
{

    public function index()
    {
        
        $list = Db::name('charge_rules')
            ->order("list_order asc")
            
            ->paginate(20)->each(function($item, $key){
                $item['money'] = json_decode($item['money'],true);
                return $item;
            });
        $page = $list->render();
        $this->assign("page", $page);
            
        $this->assign('list', $list);

        return $this->fetch();
    }


    public function add()
    {
        $country = Db::name('country')
            ->where('type',0)
            ->where('recommend',2)
            ->field('id,unit,code')
            ->select();
        $this->assign('country', $country);
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->param();
            $name=$data['name'];
            
            if($name == ''){
                $this->error('请填写名称');
            }
            
            $money=$data['money'];
            
            if(is_array($money)){
                $data['money'] = json_encode($money);
            }
            //print_r($data);exit;
            $coin=$data['coin'];
            if($coin<=0){
                $this->error('请填写正确的钻石数');
            }
            

            $id = Db::name('charge_rules')->insertGetId($data);
            if(!$id){
                $this->error("添加失败！");
            }
            $this->resetcache();
            $this->success("添加成功！");
        }
    }

    public function edit()
    {
        $id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('charge_rules')
            ->where("id={$id}")
            ->find();
        if(!empty($data['money'])){
            $money = json_decode($data['money'],true);
            $country = Db::name('country')
            ->field('id,code,unit,mark')
            ->where('type',0)
            ->where('recommend',2)
            ->select()
            ->toArray();
            foreach ($country as $k=>$v){
                 $country[$k]['money'] = 0;
                foreach ($money as $k1=>$v1){
                    if($v['code'] == $k1){
                        $country[$k]['money'] = $v1;
                    }
                }
            }
        }
        if(!$data){
            $this->error("信息错误");
        }
        if(!empty($country)){
            $this->assign('country', $country);
        }
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->param();

            $name=$data['name'];
            
            if($name == ''){
                $this->error('请填写名称');
            }
            
            $money=$data['money'];
            if(is_array($money)){
                $data['money'] = json_encode($money);
            }
            $coin=$data['coin'];
            if($coin<=0){
                $this->error('请填写正确的钻石数');
            }
            

            $rs = Db::name('charge_rules')->update($data);

            if($rs === false){
                $this->error("保存失败！");
            }
            $this->resetcache();
            $this->success("保存成功！");
        }
    }
    
    public function listOrder()
    {
        $model = Db::name('charge_rules');
        parent::listOrders($model);
        $this->resetcache();
        $this->success("排序更新成功！");
    }

    public function del()
    {
        $id = $this->request->param('id', 0, 'intval');
        
        $rs = Db::name('charge_rules')->where("id={$id}")->delete();
        if(!$rs){
            $this->error("删除失败！");
        }
        $this->resetcache();
        $this->success("删除成功！",url("Chargerule/index"));
    }


    protected function resetcache(){
        $key='getChargeRules';

        $level=Db::name('charge_rules')
                ->field('id,name,name_en,money,coin,coin_ios,coin_paypal,product_id,give')
                ->order("list_order asc")
                ->select();
        if($level){
            setcaches($key,$level);
        }
    }
}